package com.heima.controller;

import com.heima.dto.ContractPageQueryDto;
import com.heima.result.Result;
import com.heima.service.ContractService;
import com.heima.vo.ContractVo;
import com.heima.vo.PageResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/contract")
@Api(tags = "合同管理")
public class ContractController {
    @Autowired
    private ContractService contractService;

    /**
     * 分页查询合同
     */
    @GetMapping("/list")
    public PageResult list(ContractPageQueryDto contractPageQueryDTO,
                           @RequestParam(value = "params[beginCreateTime]",required = false) String begin,
                           @RequestParam(value = "params[endCreateTime]",required = false) String end) {
        contractPageQueryDTO.setBeginTime(begin);
        contractPageQueryDTO.setEndTime(end);
        log.info("分页查询合同:{}",contractPageQueryDTO);
        PageResult pageResult = contractService.pageQueryContract(contractPageQueryDTO);
        return pageResult;
    }

    /**
     * 根据合同id查看详情
     */
    @GetMapping("/detail/{id}")
    public Result<ContractVo> getById(@PathVariable("id") Long id) {
        log.info("根据合同id查看详情:{}",id);
        ContractVo contractVO = contractService.getById(id);
        return Result.success(contractVO);
    }

    /**
     * 修改合同
     */
    @PutMapping
    public Result updateContract(@RequestBody ContractVo contractVo) {
        log.info("修改合同:{}",contractVo);
        contractService.updateContract(contractVo);
        return Result.success();
    }

    /**
     * 根据id查看合同详情
     */
    @GetMapping("/{id}")
    public Result<ContractVo> getContractById(@PathVariable("id") Long id) {
        log.info("根据id查看合同详情:{}",id);
        ContractVo contractVo = contractService.getById(id);
        return Result.success(contractVo);
    }

    /**
     * 新增合同
     */
    @PostMapping
    public Result addContract(@RequestBody ContractVo contractVo) {
        log.info("新增合同:{}",contractVo);
        contractService.addContract(contractVo);
        return Result.success();
    }

}
